/*
package com.lai.qq.route.gateway.filter;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.cloud.gateway.filter.factory.rewrite.CachedBodyOutputMessage;
import org.springframework.cloud.gateway.route.Route;
import org.springframework.cloud.gateway.support.BodyInserterContext;
import org.springframework.cloud.gateway.support.DefaultServerRequest;
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpRequestDecorator;
import org.springframework.stereotype.Component;
import org.springframework.web.reactive.function.BodyInserter;
import org.springframework.web.reactive.function.BodyInserters;
import org.springframework.web.reactive.function.server.ServerRequest;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.net.URI;
import java.sql.Timestamp;

*/
/**
 * @author: lai.quanqiang
 * @date : 2020/6/30
 *//*

@Component
@Slf4j
public class ThirdRequestBodyGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> {

    @Autowired
    private CheckAuthUtils checkAuthUtils;

    @Override
    public GatewayFilter apply(Object config) {

        return new CheckAuthRequestGatewayFilter();
    }

    public class CheckAuthRequestGatewayFilter implements GatewayFilter, Ordered {


        @SneakyThrows
        @Override
        public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
            Route gatewayUrl = exchange.getRequiredAttribute(ServerWebExchangeUtils.GATEWAY_ROUTE_ATTR);
            URI uri = gatewayUrl.getUri();
            ServerHttpRequest request = (ServerHttpRequest) exchange.getRequest();
            String URIPath = request.getURI().toString();
            String path = request.getPath().value();
            String method = request.getMethodValue();
            String instance = uri.getAuthority();
            HttpHeaders header = request.getHeaders();
            Timestamp timestamp=new Timestamp(System.currentTimeMillis());//转换成系统时间时分秒
            log.info("输入参数信息:timestamp={},uri={},URIPath={},path={},method={},instance={},header={}",timestamp,uri,URIPath,
                    path,method,instance,header);
            ServerRequest serverRequest = new DefaultServerRequest(exchange);
            // TODO: flux or mono
            // mediaType
            MediaType mediaType = exchange.getRequest().getHeaders().getContentType();
            // read & modify body
            Mono<String> modifiedBody = serverRequest.bodyToMono(String.class).flatMap(body -> {
                if (MediaType.APPLICATION_JSON.isCompatibleWith(mediaType)) {
                    // TODO decrypt & auth
                    try {
                        //第三方平台拦截url处理报文返回再路由带后端服务
                        body = checkAuthUtils.appFirstDecrypt(body);//app发来在数据进行第一层解密
                        body =  checkAuthUtils.verify(body);
                    } catch (Exception e) {
                        throw new RuntimeException("请求参数为空或检验不通过");
                    }
                    return Mono.just(body);
                }
                return Mono.empty();
            });

            BodyInserter bodyInserter = BodyInserters.fromPublisher(modifiedBody, String.class);
            HttpHeaders headers = createHeader();
            headers.putAll(exchange.getRequest().getHeaders());
            headers.remove("Authorization");
            headers.remove(HttpHeaders.CONTENT_LENGTH);

            CachedBodyOutputMessage outputMessage = new CachedBodyOutputMessage(exchange, headers);
            return bodyInserter.insert(outputMessage, new BodyInserterContext()).then(Mono.defer(() -> {
                ServerHttpRequestDecorator decorator = new ServerHttpRequestDecorator(exchange.getRequest()) {

                    @Override
                    public HttpHeaders getHeaders() {
                        HttpHeaders httpHeaders = new HttpHeaders();
                        httpHeaders.putAll(headers);
                        return httpHeaders;
                    }

                    @Override
                    public Flux<DataBuffer> getBody() {
                        return outputMessage.getBody();
                    }

                };
                return chain.filter(exchange.mutate().request(decorator).build());
            }));

        }

        */
/**
         * 生成消息头
         *//*

        public HttpHeaders createHeader() {
            HttpHeaders headers = new HttpHeaders();
            headers.setContentType(MediaType.APPLICATION_JSON);
            return headers;
        }

        @Override
        public int getOrder() {
            return 0;
        }

    }


}
*/
